﻿<Cmdlet(VerbsCommon.Close, "SqlConnection")>
Public Class CloseSqlConnection
    Inherits PSCmdlet

    <Parameter(ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, Position:=0)>
    Public Property ConnectionName As String = "Default"

    Protected Overrides Sub ProcessRecord()
        MyBase.ProcessRecord()

        If DBInfo.Connections.ContainsKey(ConnectionName) Then
            DBInfo.Connections(Me.ConnectionName).RollbackTransaction(True)
            If DBInfo.Connections(Me.ConnectionName).Connection.State = ConnectionState.Open Then DBInfo.Connections(ConnectionName).Connection.Close()
            DBInfo.Connections(Me.ConnectionName).Connection.Dispose()
            DBInfo.Connections.Remove(Me.ConnectionName)
        Else
            WriteWarning(DBInfo.NoConnection(Me.ConnectionName))
        End If
    End Sub
End Class
